home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Science / RLaB / examples / euclid.r < prev    next >
Text File  |  1994-04-25  |  492b  |  29 lines

  1. //-------------------------------------------------------------------//
  2. //  Euclid's algorithm
  3. //
  4.  
  5. //  Syntax:    euclid ( M , N )
  6.  
  7. //  Description:
  8.  
  9. //  Euclid's algorithm computes the greatest common divisor of two
  10. //  integers (M, and N).
  11. //-------------------------------------------------------------------//
  12.  
  13. euclid = function ( M , N )
  14. {
  15.   local( m , n , r );
  16.  
  17.   m = M;
  18.   n = N;
  19.   r = 1;
  20.   while(r)
  21.   {
  22.     r = mod(m,n);
  23.     if(r == 0) { break; }
  24.     m = n;
  25.     n = r;
  26.   }
  27.   return n;
  28. };
  29.